<?php


namespace app\master\controller;

use app\common\Base;
use app\model\ContentCategoryModel;
use app\model\ContentModel;
use app\server\Upload;
use think\facade\Validate;

class Content extends Base {

    /**
     * 内容列表
     * @return \think\response\Json|\think\response\View
     */
    public function content_list() {
        list($where, $page, $limit) = getData();
        $whereLike = [];
        if (isset($where['title'])) {
            $whereLike[] = ['title', 'like', '%' . $where['title'] . '%'];
            unset($where['title']);
        }
        list($list, $count) = ContentModel::getList($where, $page, $limit, $whereLike);
        return success('ok', $list, $count);
    }

    /**
     * 获取文章分类
     */
    public function get_category() {
        $list = ContentCategoryModel::getSelectList();
        return success('ok', $list);
    }

    /**
     * 添加内容
     * @return \think\response\Json|\think\response\View
     */
    public function content_add() {
        $postData = $this->request->post([
            'title',
            'remark',
            'images',
            'cate_id',
            'content',
        ]);
        $validate = Validate::rule([
            "title|标题" => "require|min:5",
            "remark|描述" => "require",
            "cate_id|分类" => "require|integer",
            "images|图片" => "require",
            "content|文章内容" => "require",
        ]);
        if (!$validate->check($postData)) {
            return error($validate->getError());
        }
        $postData['create_time'] = $this->Time;
        ContentModel::insert($postData);
        return success('添加内容成功');
    }

    /**
     * 编辑内容
     * @return \think\response\Json
     */
    public function content_edit() {
        $postData = $this->request->post([
            'title',
            'remark',
            'cate_id',
            'images',
            'content',
            'id',
        ]);
        $validate = Validate::rule([
            "title|标题" => "require|min:5",
            "remark|描述" => "require",
            "cate_id|分类" => "require|integer",
            "images|图片" => "require",
            "content|文章内容" => "require",
            "id|数据标识" => "require|integer",
        ]);
        if (!$validate->check($postData)) {
            return error($validate->getError());
        }
        ContentModel::update($postData);
        return success('编辑内容成功');
    }

    /**
     * 处理内容状态
     * @return \think\response\Json
     */
    public function content_status() {
        $postData = $this->request->post([
            'id',
            'status',
            'is_del',
        ]);
        $validate = Validate::rule([
            "is_del|删除情况" => "integer",
            "status|展示情况" => "integer",
            "id|数据标识" => "require|integer",
        ]);
        if (!$validate->check($postData)) {
            return error($validate->getError());
        }
        if (isset($postData['status']) || isset($postData['is_del'])) {
            ContentModel::update($postData);
            return success('编辑内容成功');
        }
        return error('缺少参数');
    }
}
